package com.zzc.dao;

import java.util.List;

import com.zzc.bean.Employee;
import org.hibernate.Query;

import com.google.gson.Gson;
import org.springframework.stereotype.Repository;

public class EmployeeDao extends BaseDao{
	public void delete(int employee_id) {
		System.out.println(employee_id);
		String hql = "delete Employee employee WHERE employee.employee_id = ?";
		Query query = getSession().createQuery(hql).setInteger(0, employee_id);
		query.executeUpdate();
	}
	public void alterEmployee() {

	}
	public List<Employee> getAll(){
		String hql = "FROM Employee";//hql = "FROM Employee";
		List<Employee> list_employees =getSession().createQuery(hql).list();
		String string3=new Gson().toJson(list_employees);

		System.out.println(string3);
		return list_employees;
	}
	public Employee getEmployee(String employee_account) {
		String hql = "FROM Employee employee WHERE employee.employee_account = ?";
		Query query = getSession().createQuery(hql).setString(0, employee_account);
		Employee employee = (Employee) query.uniqueResult();
		//System.out.println(employee.getDepartment().getClass().getName());
		String string=new Gson().toJson(employee);
		System.out.println(string);
		return employee;
	}
	public Employee get_id(int employee_id) {
		String hql = "FROM Employee employee WHERE employee.employee_id = ?";
		Query query = getSession().createQuery(hql).setInteger(0, employee_id);
		Employee employee = (Employee) query.uniqueResult();
		return employee;
	}
	public Employee get_account(String employee_account) {
		String hql = "FROM Employee employee WHERE employee.employee_account = ?";
		Query query = getSession().createQuery(hql).setString(0, employee_account);
		Employee employee = (Employee) query.uniqueResult();
		return employee;
	}
	public Employee login(String employee_account, String employee_password){
		String hql_getUserCount = "from Employee";
		Query query_getUserCount = getSession().createQuery(hql_getUserCount);
		List<Employee>  employee_list = query_getUserCount.list();
		int userCount=employee_list.size();
		System.out.println("userCount"+String.valueOf(userCount));
		for (int i=0;i<userCount;i++){
			Employee employee=employee_list.get(i);
			System.out.println("employee.getEmployee_name():"+employee.getEmployee_name());
		}
		if (userCount==0){
			System.out.println("检测到没有任何一个用户，开始创建admin用户，密码admin");
			Employee employee_admin=new Employee("admin","admin","admin","admin","admin","admin","admin");
			getSession().saveOrUpdate(employee_admin);
			System.out.println("创建成功...");
		}
		String hql = "FROM Employee employee WHERE employee.employee_account = ? and employee.employee_password = ?";
		Query query = getSession().createQuery(hql).setString(0, employee_account).setString(1, employee_password);
		Employee employee = (Employee) query.uniqueResult();
		String string=new Gson().toJson(employee);
		System.out.println(string);
		return employee;
	}
}
